var $_GET = (function() {
    var url = window.document.location.href.toString();
    var u = url.split("?");
    if (typeof(u[1]) == "string") {
        u = u[1].split("&");
        var get = {};
        for (var i in u) {
            var j = u[i].split("=");
            get[j[0]] = j[1];
        }
        return get;
    } else {
        return {};
    }
})();
if ($_GET['buyok']) {
    var id = $("#order_all>.layui-card:first").find(".xiangqing").data("id");
    var skey = $("#order_all>.layui-card:first").find(".xiangqing").data("skey");
    if (id) {
        showOrder(id, skey, true);
    }

}

function OrderQuery() {
    var kw = $('#query').val();
    window.location.href = "./?mod=query&data=" + kw;
}

function LastPage() {
    var kw = $('#query').val();
    var page = parseInt($('#page').val());
    var status = $('#q_status').val();
    if (page == '1') return;
    page = page - 1;
    window.location.href = "./?mod=query&status=" + status + "&data=" + kw + "&page=" + page;
}

function NextPage() {
    var kw = $('#query').val();
    var page = parseInt($('#page').val());
    var status = $('#q_status').val();
    page = page + 1;
    window.location.href = "./?mod=query&status=" + status + "&data=" + kw + "&page=" + page;
}

function changepwd(id, skey) {
    pwdlayer = layer.open({
        type: 1,
        title: '修改密码',
        skin: 'layui-layer-rim',
        content: '<div class="form-group"><div class="bl_view_title"><div class="input-group-addon">密码</div><input type="text" id="pwd" value="" class="search_input2" placeholder="请填写新的密码" required/></div></div><div class="go_buy"><input type="submit" id="save" onclick="saveOrderPwd(' + id + ',\'' + skey + '\')" class="btn btn-primary btn-block" value="保存"></div>'
    });
}

function saveOrderPwd(id, skey) {
    var pwd = $("#pwd").val();
    if (pwd == '') { layer.alert('请确保每项不能为空！'); return false; }
    var ii = layer.load(2, { shade: [0.1, '#fff'] });
    $.ajax({
        type: "POST",
        url: "ajax.php?act=changepwd",
        data: { id: id, pwd: pwd, skey: skey },
        dataType: 'json',
        success: function(data) {
            layer.close(ii);
            if (data.code == 0) {
                layer.msg('保存成功！');
                layer.close(pwdlayer);
            } else {
                layer.alert(data.msg);
            }
        }
    });
}

function showOrder(id, skey, flag) {
    if (flag) {
        return;
    }
    console.log(id, skey, flag)
    var ii = layer.load(2, { shade: [0.1, '#fff'] });
    var status = ['<span class="label label-primary">待处理</span>', '<span class="label label-success">已完成</span>', '<span class="label label-warning">处理中</span>', '<span class="label label-danger">异常</span>', '<font color=red>已退款</font>'];
    $.ajax({
        type: "POST",
        url: "ajax.php?act=order",
        data: { id: id, skey: skey },
        dataType: 'json',
        success: function(data) {
            layer.close(ii);
            if (data.code == 0) {
                var item = '<table class="table table-condensed table-hover" id="orderItem">';
                item += '<tr><td colspan="6" style="text-align:center" class="orderTitle"><b>订单基本信息</b></td></tr><tr><td class="info orderTitle">订单编号</td><td colspan="5" class="orderContent">' + id + '</td></tr><tr><td class="info orderTitle">商品名称</td><td colspan="5" class="orderContent">' + data.name + '</td></tr><tr><td class="info orderTitle">订单金额</td><td colspan="5" class="orderContent">' + data.money + '元</td></tr><tr><td class="info orderTitle">购买时间</td><td colspan="5">' + data.date + '</td></tr><tr><td class="info orderTitle">下单信息</td><td colspan="5" class="orderContent">' + data.inputs + '</td><tr><td class="info orderTitle">订单状态</td><td colspan="5" class="orderContent">' + status[data.status] + '</td></tr>';
                if (data.complain) {
                    item += '<tr><td class="info orderTitle">订单操作</td><td class="orderContent"><a href="./user/workorder.php?my=add&orderid=' + id + '&skey=' + skey + '" target="_blank" onclick="return checklogin(' + data.islogin + ')" class="btn btn-xs btn-default">投诉订单</a>';
                    if (data.selfrefund == 1 && data.islogin == 1 && (data.status == 0 || data.status == 3)) {
                        item += '&nbsp;<a onclick="return apply_refund(' + id + ',\'' + skey + '\')" class="btn btn-xs btn-danger">申请退款</a>';
                    }
                    item += '</td></tr>';
                }
                if (data.list && typeof data.list === "object") {
                    if (typeof data.list.order_state !== "undefined" && data.list.order_state && typeof data.list.now_num !== "undefined") {
                        item += '<tr><td colspan="6" style="text-align:center" class="orderTitle"><b>订单实时状态</b></td><tr><td class="warning">下单数量</td><td>' + data.list.num + '</td><td class="warning">下单时间</td><td colspan="3">' + data.list.add_time + '</td></tr><tr><td class="warning">初始数量</td><td>' + data.list.start_num + '</td><td class="warning">当前数量</td><td>' + data.list.now_num + '</td><td class="warning">订单状态</td><td><font color=blue>' + data.list.order_state + '</font></td></tr>';
                        if (typeof data.list.result !== "undefined" && data.list.result) {
                            item += '<tr><td class="warning orderTitle">异常信息</td><td class="orderContent">' + data.list.result + '</td></tr>';
                        }
                    } else {
                        item += '<tr><td colspan="6" style="text-align:center" class="orderTitle"><b>订单实时状态</b></td>';
                        $.each(data.list, function(i, v) {
                            item += '<tr><td class="warning orderTitle">' + i + '</td><td class="orderContent">' + v + '</td></tr>';
                        });
                    }
                } else if (data.kminfo) {
                    item += '<tr><td colspan="6" style="text-align:center" class="orderTitle"><b>以下是你的卡密信息</b></td><tr><td colspan="6" class="orderContent">' + data.kminfo + '</td></tr>';
                } else if (data.result) {
                    item += '<tr><td colspan="6" style="text-align:center" class="orderTitle"><b>处理结果</b></td><tr><td colspan="6" class="orderContent">' + data.result + '</td></tr>';
                }
                if (data.desc) {
                    item += '<tr><td colspan="6" style="text-align:center" class="orderTitle"><b>商品简介</b></td><tr><td colspan="6" class="orderContent">' + data.desc + '</td></tr>';
                }
                item += '</table>';
                var area = [$(window).width() > 480 ? '480px' : '100%', ';max-height:100%'];
                layer.open({
                    type: 1,
                    area: area,
                    title: '订单详细信息',
                    skin: 'layui-layer-rim',
                    zIndex: 2001,
                    content: item
                });
            } else {
                layer.alert(data.msg);
            }
        }
    });
}

function checklogin(islogin) {
    if (islogin == 1) {
        return true;
    } else {
        var confirmobj = layer.confirm('为方便反馈处理结果，投诉订单前请先登录网站！', {
            btn: ['登录', '注册', '取消']
        }, function() {
            window.location.href = './user/login.php';
        }, function() {
            window.location.href = './user/reg.php';
        }, function() {
            layer.close(confirmobj);
        });
        return false;
    }
}

function apply_refund(id, skey) {
    var confirmobj = layer.confirm('待处理或异常状态订单可以申请退款，退款之后资金会退到用户余额，是否确认退款？', {
        btn: ['确认退款', '取消']
    }, function() {
        var ii = layer.load(2, { shade: [0.1, '#fff'] });
        $.ajax({
            type: "POST",
            url: "ajax.php?act=apply_refund",
            data: { id: id, skey: skey },
            dataType: 'json',
            success: function(data) {
                layer.close(ii);
                if (data.code == 0) {
                    layer.alert('成功退款' + data.money + '元到余额！', { icon: 1 }, function() { window.location.reload(); });
                } else {
                    layer.alert(data.msg, { icon: 2 });
                }
            }
        });
    }, function() {
        layer.close(confirmobj);
    });
}